package com.cty.B_SpecialDataStructures.A_Stack.StackChain;

import com.cty.F_common.DataItem;

/**
 * @Auther: cty
 * @Date: 2020/6/13 23:09
 * @Description:  单链表（单端单向）
 * 重要操作：
 *      ① insertFirst(DataItem): void    O(1)
 *      ② deleteFirst(): DataItem    O(1)
 * @version: 1.0
 */
class LinkedList{
    private Node first;

    public LinkedList(){
        first = null;
    }

    public Node getFirst(){
        return first;
    }

    public boolean isEmpty(){
        return (first == null);
    }

    public void displayList(){
        Node current = first;
        while(current != null) {
            System.out.print(current.data.getKey() + " ");
            current = current.next;
        }
        System.out.println();
    }

    public void insertFirst(DataItem item){
        Node newNode = new Node(item);
        newNode.next = first;
        first = newNode;
    }

    public DataItem deleteFirst(){
        if(isEmpty())
            return null;

        DataItem temp = first.data;
        first = first.next;
        return temp;
    }
}  // end LinkedList{}
